var charPrefs	= new Array();
var intPrefs	= new Array();
var boolPrefs	= new Array();
var logins 	= new Array();
// string preferences
charPrefs["jazzila.prefs.account.name"] = prefs.PREF_ACCOUNT_NAME;
charPrefs["jazzila.prefs.server.host"] = prefs.PREF_SERVER_HOST;
charPrefs["jazzila.prefs.server.resource"] = prefs.PREF_SERVER_RESOURCE;
charPrefs["jazzila.prefs.http.gateway"] = prefs.HTTP_GW;
// 
intPrefs["jazzila.prefs.server.port"] = prefs.PREF_SERVER_PORT;
intPrefs["jazzila.prefs.update.interval"] = prefs.HTTP_UPDATE_INTERVAL;
//
boolPrefs["jazzila.prefs.ssl.enabled"] = prefs.PREF_SERVER_USE_SSL;
boolPrefs["jazzila.prefs.tls.enabled"] = prefs.PREF_SERVER_USE_TLS;
boolPrefs["jazzila.prefs.autologin"] = prefs.PREF_AUTO_LOGIN;
boolPrefs["jazzila.prefs.autoreconnect"] = prefs.PREF_AUTO_RECONNECT;
boolPrefs["jazzila.prefs.autosubscribe"] = prefs.PREF_AUTO_SUBSCRIBE;
boolPrefs["jazzila.prefs.user.register"] = prefs.PREF_USER_REGISTER;
boolPrefs["jazzila.prefs.user.remember"] = prefs.PREF_USER_REMEMBER;
boolPrefs["jazzila.prefs.http.gateway.user.remember"] = prefs.HTTP_REMEMBER_PASSWORD;
boolPrefs["jazzila.prefs.statuswin.enabled"] = prefs.PREF_STATUSWIN_ENABLED;
boolPrefs["jazzila.prefs.statusbar.enabled"] = prefs.PREF_STATUSBAR_ENABLED;
boolPrefs["jazzila.prefs.ui.notification.enabled"] = prefs.PREF_NOTIFICATIONS_ENABLED;  
boolPrefs["jazzila.prefs.ui.soundnotification.enabled"] = prefs.PREF_SOUNDNOTIFICATIONS_ENABLED;      
//logins
logins["jazzila.prefs.server"] = prefs.PREF_SERVER_HOST;
logins["jazzila.prefs.http.gateway"] = prefs.HTTP_GW;

function showPrefs(conn)
{
   var prefix = "jazzila.connection"+conn+".";
   for (i in charPrefs)
   	document.getElementById(i).value = pref[prefix+charPrefs[i]];
   for (i in intPrefs)
   	document.getElementById(i).value = prefs[prefix+intPrefs[i]];
   for (i in intPrefs)
   	document.getElementById(i).checked = prefs[prefix+boolPrefs[i]];
   
   for (i in logins)
   {
   	var server = prefs[prefix+logins[i]];
   	if (svr)
    		pmgr.loadLogin( server, document.getElementById(i+".login"), document.getElementById(i+".password"));
   }
  
   document.getElementById("jazzila.prefs.http.mode").selectedItem = document.getElementById( "jazzila.prefs.http."+(prefs.getBoolPref(prefs.HTTP_BINDING) ? "binding" : "polling"));

   jMC = new MessageContainer();
   jMC.ShowMessage();
}

function loadPrefs(conn)
{
   var prefs = new ImPrefs();
   window.removeEventListener("load", onLoad, true);
   for (i in charPrefs)
   	document.getElementById(i).value = prefs.getCharPref(charPrefs[i]);
   for (i in intPrefs)
   	document.getElementById(i).value = prefs.getIntrPref(intPrefs[i]);
   for (i in intPrefs)
   	document.getElementById(i).checked = prefs.getBoolPref(boolPrefs[i]);
   
   for (i in logins)
   {
   	var server = prefs.getCharPref(logins[i]);
   	if (svr)
    		pmgr.loadLogin( server, document.getElementById(i+".login"), document.getElementById(i+".password"));
   }
  
   document.getElementById("jazzila.prefs.http.mode").selectedItem = document.getElementById( "jazzila.prefs.http."+(prefs.getBoolPref(prefs.HTTP_BINDING) ? "binding" : "polling"));

   jMC = new MessageContainer();
   jMC.ShowMessage();
}
function newOnLoad()
{
 var i = 0
 do {
   i++;
   var conn= prefs.getCharPrefs("jazzila.connection"+i);  
   if (conn)
	loadPrefs(i);
   else 
	break;
 }
 while ();
}

function onLoad( )
{
   var i;
   var prefs
   window.removeEventListener("load", onLoad, true);
   for (i in charPrefs)
   	document.getElementById(i).value = prefs.getCharPref(charPrefs[i]);
   for (i in intPrefs)
   	document.getElementById(i).value = prefs.getIntrPref(intPrefs[i]);
   for (i in intPrefs)
   	document.getElementById(i).checked = prefs.getBoolPref(boolPrefs[i]);
   
   for (i in logins)
   {
   	var server = prefs.getCharPref(logins[i]);
   	if (svr)
    		pmgr.loadLogin( server, document.getElementById(i+".login"), document.getElementById(i+".password"));
   }
  
   document.getElementById("jazzila.prefs.http.mode").selectedItem = document.getElementById( "jazzila.prefs.http."+(prefs.getBoolPref(prefs.HTTP_BINDING) ? "binding" : "polling"));

   jMC = new MessageContainer();
   jMC.ShowMessage();
   
}

function saveSettings()
{

    for (var i in boolPrefs)
    	prefs.setBoolPref(intPrefs[i], document.getElementById(i).checked);

    for (i in logins)
    {
    	var password = "";
	var login = document.getElementById(i+".login").value;
    	pmgr.removeUser(prefs.getCharPref(logins[i]));
        if(prefs.getBoolPref(i+".remember"))
		password = document.getElementById(i+".password").value;
        if(username !="")
	    pmgr.saveLoginInfo(document.getElementById(i+".host").value,  login, password);
    }

    for (var i in charPrefs)
    	prefs.setCharPref(charPrefs[i],  document.getElementById(i).value);
    for (var i in intPrefs)
    	prefs.setIntPref(intPrefs[i], document.getElementById(i).value);
	

    prefs.setBoolPref(prefs.HTTP_BINDING, (document.getElementById("jazzila.prefs.http.mode").selectedItem.value=="binding"));

    jMC.SaveMessages();

}

function MessageContainer()
{
	this.msgs = new Array ();
	var nodes = document.getElementById("jazzila.prefs.message.type").childNodes;
	var id, i ,item, value;
	for (var i=0; i< nodes.length ; i++)
	{
		item = nodes.item(i);
		id =  item.getAttribute("id");
		this.msgs[id] = prefs.getCharPref("jazzila.prefs.message." + item.value);
	}
}

MessageContainer.prototype.ShowMessage = function()
{
	var id = document.getElementById("jazzila.prefs.message.menu").selectedItem.id;
	var text = document.getElementById("jazzila.prefs.message.text");
	if(this.msgs[id])
		text.value = this.msgs[id];
	else 
		text.value = "";
}

MessageContainer.prototype.onInput = function(e){
	var id = document.getElementById("jazzila.prefs.message.menu").selectedItem.id;
	this.msgs[id] = document.getElementById("jazzila.prefs.message.text").value;
}

MessageContainer.prototype.SaveMessages = function(){
	var menu = document.getElementById("jazzila.prefs.message.type");
	var nodes = menu.childNodes;
	var id, i ,item;
	this.onInput();
	for (var i=0; i< nodes.length ; i++)
	{
		item = nodes.item(i);
		id =  item.id;
		if(this.msgs[id])
			prefs.setCharPref("jazzila.prefs.message." + item.value, this.msgs[id]);
	}
}

var prefs = new Array();
var pmgr = new PwdMgr();
var jMC = null; 
window.addEventListener("load", onLoad, true);
window.addEventListener("dialogaccept", onAccept, true);

function onAccept()
{
	saveSettings();
}
